package com.toune.villager.fragment

import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.just.agentweb.AgentWeb
import com.toune.villager.R
import com.toune.villager.adapter.MyBannerAdapter
import com.toune.villager.bean.BannerDataBean
import com.toune.villager.model.HomeFragmentModel
import com.toune.dltools.ui.DLBaseFragment
import com.toune.villager.activity.GoodsListActivity
import com.toune.villager.adapter.GoodsSubTypeAdapter
import com.toune.villager.adapter.GoodsTypeAdapter
import com.toune.villager.bean.GoodsTypeBean
import com.toune.villager.model.LifeModel
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_life.*
import kotlinx.android.synthetic.main.layout_goods_type.*


class LifeFragment : DLBaseFragment<LifeModel>() {
    override val layout: Int
        get() = R.layout.fragment_life

    override fun lazyInit() {
        mViewModel.getGoodsType()
        swipeRefreshLv.setOnRefreshListener {
            mViewModel.getGoodsType()
        }
        mViewModel.goodsTypeBeanValue.observe(this, {
            swipeRefreshLv.isRefreshing = false
            goodsTypeDataBeans.clear()
            goodsTypeDataBeans.addAll(it)
            setGoodsTypeAdapter()
            if (goodsTypeDataBeans.size > 0) {
                setGoodsSubTypeAdapter(goodsTypeDataBeans[selectedGoodsTypePos].dlGoodsSubTypeList)
            }
        })
    }

    var goodsTypeAdapter: GoodsTypeAdapter? = null
    var goodsSubTypeAdapter: GoodsSubTypeAdapter? = null
    val goodsTypeDataBeans: MutableList<GoodsTypeBean.RowsBean> = ArrayList()
    val goodsSubTypeDataBeans: MutableList<GoodsTypeBean.RowsBean.DlGoodsSubTypeListBean> =
        ArrayList()

    var selectedGoodsTypePos = 0
    fun setGoodsTypeAdapter() {
        if (goodsTypeAdapter == null) {
            goodsTypeAdapter = GoodsTypeAdapter(R.layout.adapter_good_type, goodsTypeDataBeans)
            leftRclView.layoutManager = LinearLayoutManager(context)
            leftRclView.adapter = goodsTypeAdapter
            goodsTypeAdapter!!.setOnItemClickListener { adapter, view, position ->
                selectedGoodsTypePos = position
                goodsTypeAdapter!!.setSelectPos(position)
                subTypeTitleTv.text = goodsTypeDataBeans[position].name
                setGoodsSubTypeAdapter(goodsTypeDataBeans[position].dlGoodsSubTypeList)
            }
        } else {
            goodsTypeAdapter!!.notifyDataSetChanged()
        }
    }


    fun setGoodsSubTypeAdapter(dataBeans: MutableList<GoodsTypeBean.RowsBean.DlGoodsSubTypeListBean>) {
        goodsSubTypeDataBeans.clear()
        goodsSubTypeDataBeans.addAll(dataBeans)
        if (goodsSubTypeAdapter == null) {
            goodsSubTypeAdapter =
                GoodsSubTypeAdapter(R.layout.adapter_good_sub_type, goodsSubTypeDataBeans)
            rightRclView.layoutManager = LinearLayoutManager(context)
            rightRclView.adapter = goodsSubTypeAdapter
            goodsSubTypeAdapter!!.setOnItemClickListener { adapter, view, position ->
                var bundle = Bundle()
                bundle.putInt("goodsTypeId", goodsSubTypeDataBeans[position].id)
                bundle.putString("goodsTypeName", goodsSubTypeDataBeans[position].name)
                startToActivity(GoodsListActivity::class.java, bundle)
            }
        } else {
            goodsSubTypeAdapter!!.notifyDataSetChanged()
        }
    }
}